<?php
namespace App\Handlers;

use GuzzleHttp\Client;
use Image;
use App\Services\OSS;

class PictuerAverageHandler
{
  public function get($pictuers)
  {
    $averages = [];
    foreach($pictuers as $pictuer) {
      $averages[$pictuer['id']]['rgb'] = $this->getByUrl('/' . $pictuer['path'] . '/' . $pictuer['filename']);
    }
    return $averages;
  }

  public function getByUrl($url)
  {
      if (config('app.env') === 'local') {
        $http_url = 'http://touxiangge.oss-cn-shenzhen.aliyuncs.com';
      } else {
        $http_url = 'http://touxiangge.oss-cn-shenzhen-internal.aliyuncs.com';
      }

      $http = new Client;
      $response = $http->get($http_url.$url . '?x-oss-process=image/average-hue');
      $result = json_decode($response->getBody(), true);
      /*  得到的结果
      [
        'RGB' => "0x5c783b"
      ]
      */
      //return $result;
      return $result['RGB'];
  }
}
